<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="经费编号" label-width="140px" prop="fsubNo">
            <el-input
              v-model="queryParams.fsubNo"
              placeholder="请输入经费编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="经费名称" label-width="140px" prop="fsubName">
            <el-input
              v-model="queryParams.fsubName"
              placeholder="请输入经费名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="经费类别" label-width="140px" prop="fsubType">
            <!-- 01.教学 02.纵向调研 03.横向调研 04.专项 -->
            <el-select
              v-model="queryParams.fsubType"
              placeholder="请选择经费类别"
              clearable
              @keyup.enter.native="handleQuery"
            >
              <el-option
                v-for="dict in dict.type.sys_jfsb_fsub_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="审核状态" label-width="140px" prop="state">
            <!-- 0.未审 1.审核中 2.审核完结 3.驳回 4.采购执行 5.合同签订 6.到货验收 -->
            <el-select
              v-model="queryParams.state"
              value=""
              placeholder="请选择审核状态"
              clearable
              @keyup.enter.native="handleQuery"
            >
              <el-option
                v-for="dict in dict.type.sys_jfsb_state"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="预算年度" label-width="140px" prop="yusnd">
            <el-date-picker
              clearable
              v-model="queryParams.yusnd"
              type="year"
              value-format="yyyy"
              placeholder="请选择预算年度"
              @keyup.enter.native="handleQuery"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="预算金额" label-width="140px" prop="budget">
            <el-input
              v-model="queryParams.budget"
              placeholder="请输入预算金额"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="queryCollapse.open">
        <el-col :span="8">
          <el-form-item label="归属部门" label-width="140px" prop="deptNo" >
            <treeselect v-model="queryParams.deptNo" :options="deptOptions" :show-count="true" placeholder="请选择部门" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="申请人编号" label-width="140px" prop="appPerNo">
            <el-input
              v-model="queryParams.appPerNo"
              placeholder="请输入申请人编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="申请人姓名" label-width="140px" prop="appPer">
            <el-input
              v-model="queryParams.appPer"
              placeholder="请输入申请人姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="联系电话" label-width="140px" prop="tel">
            <el-input
              v-model="queryParams.tel"
              placeholder="请输入联系电话"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="申请类别" label-width="140px" prop="rtNo">
            <!-- 01.常规类申报 02.大型仪器申报 03.科研零散类申报 -->
            <el-select
              v-model="queryParams.rtNo"
              placeholder="请选择申请类别"
              clearable
              @keyup.enter.native="handleQuery"
            >
              <el-option
                v-for="dict in dict.type.sys_jfsb_rt_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="申报理由" label-width="140px" prop="applyReason">
            <el-input
              v-model="queryParams.applyReason"
              placeholder="请输入申报理由"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="申请部门负责人工号" label-width="140px" prop="fzrNo">
            <el-input
              v-model="queryParams.fzrNo"
              placeholder="请输入申请部门负责人工号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="申请部门负责人姓名" label-width="140px" prop="fzrName">
            <el-input
              v-model="queryParams.fzrName"
              placeholder="请输入申请部门负责人姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="是否进口" label-width="140px" prop="isImport">
            <!-- 0.国产 1.进口 -->
            <el-select
              v-model="queryParams.isImport"
              placeholder="请选择是否进口"
              clearable
              @keyup.enter.native="handleQuery"
            >
              <el-option
                v-for="dict in dict.type.sys_jfsb_is_import"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目负责人姓名" label-width="140px" prop="fsubFzrName">
            <el-input
              v-model="queryParams.fsubFzrName"
              placeholder="请输入项目负责人姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目负责人工号" label-width="140px" prop="fsubFzrNo">
            <el-input
              v-model="queryParams.fsubFzrNo"
              placeholder="请输入项目负责人工号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="申请日期" label-width="140px" prop="appDate">
            <el-date-picker
              clearable
              unlink-panels
              v-model="queryParams.appDateArray"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6" :offset="6">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            <el-button :icon="queryCollapse.icon" type="text" @click="openMoreParam">{{ queryCollapse.content }}</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yscggl:jfsb:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yscggl:jfsb:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yscggl:jfsb:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yscggl:jfsb:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" :columns="columns" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="jfsbList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" v-if="columns[0].visible"/>
      <el-table-column label="经费编号" align="center" prop="fsubNo" v-if="columns[1].visible"/>
      <el-table-column label="经费名称" align="center" prop="fsubName" v-if="columns[2].visible"/>
      <el-table-column label="经费类别" align="center" prop="fsubType" v-if="columns[3].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_jfsb_fsub_type" :value="scope.row.fsubType"/>
        </template>
      </el-table-column>
      <el-table-column label="部门" align="center" prop="deptNo" v-if="columns[4].visible"/>
      <el-table-column label="申请人" align="center" prop="appPerNo" v-if="columns[5].visible"/>
      <el-table-column label="联系电话" align="center" prop="tel" v-if="columns[6].visible"/>
      <el-table-column label="申请日期" align="center" prop="appDate" width="180" v-if="columns[7].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.appDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="state" v-if="columns[8].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_jfsb_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="申请类别" align="center" prop="rtNo" v-if="columns[9].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_jfsb_rt_type" :value="scope.row.rtNo"/>
        </template>
      </el-table-column>
      <el-table-column label="申报理由" align="center" prop="applyReason" v-if="columns[10].visible"/>
      <el-table-column label="预算金额" align="center" prop="budget" v-if="columns[11].visible"/>
      <el-table-column label="部门负责人" align="center" prop="fzrNo" v-if="columns[12].visible"/>
      <el-table-column label="是否进口" align="center" prop="isImport" v-if="columns[13].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_jfsb_is_import" :value="scope.row.isImport"/>
        </template>
      </el-table-column>
      <el-table-column label="项目负责人" align="center" prop="fsubFzrNo" v-if="columns[14].visible"/>
      <el-table-column label="预算年度" align="center" prop="yusnd" v-if="columns[15].visible"/>
      <el-table-column label="备注" align="center" prop="remark" v-if="columns[16].visible"/>
      <el-table-column fixed="right" width="180" label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-check"
            @click="handleShow(scope.row)"
            v-if="scope.row.state == '1'"
            v-hasPermi="['yscggl:jfsb:check']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.state == '0'"
            v-hasPermi="['yscggl:jfsb:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.state == '0'"
            v-hasPermi="['yscggl:jfsb:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleShow(scope.row)"
            v-hasPermi="['yscggl:jfsb:query']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改经费申报对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24" v-if="addOrUpdate">
            <el-form-item label="经费编号" prop="fsubNo">
              <el-input v-model="form.fsubNo" placeholder="请输入经费编号" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="经费名称" prop="fsubName">
              <el-input v-model="form.fsubName" placeholder="请输入经费名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请类别" prop="rtNo">
              <el-select
                v-model="form.rtNo"
                placeholder="请选择申请类别"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.sys_jfsb_rt_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门负责人" prop="fzrNo">
              <el-input v-model="form.fzrNo" placeholder="请选择部门负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="申报理由" prop="applyReason">
              <el-input v-model="form.applyReason" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经费类别" prop="fsubType">
              <el-select
                v-model="form.fsubType"
                placeholder="请选择经费类别"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.sys_jfsb_fsub_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否进口" prop="isImport">
              <!-- 0.国产 1.进口 -->
              <el-select
                v-model="form.isImport"
                placeholder="请选择是否进口"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.sys_jfsb_is_import"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预算年度" prop="yusnd">
              <el-date-picker
                clearable
                v-model="form.yusnd"
                type="year"
                value-format="yyyy"
                placeholder="请选择预算年度"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预算金额" prop="budget">
              <el-input v-model="form.budget" placeholder="请输入预算金额" readonly>
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目负责人" prop="fsubFzrNo">
              <el-input v-model="form.fsubFzrNo" placeholder="请选择项目负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-card v-for="(item, index) in form.items">
          <div slot="header" class="clearfix">
            <span>{{ '经费申报明细' + (index + 1) }}</span>
            <el-button :loading="buttonLoading" icon="el-icon-remove-outline" style="float: right" type="text" size="medium" @click="removeItem(item)" circle>
              删除明细
            </el-button>
          </div>
          <el-row>
            <el-col :span="12">
              <el-input v-model="item.id" style="display: none" type="hidden"/>
              <el-form-item label="预算资产名称" prop="asName">
                <el-input v-model="item.asName" placeholder="请输入预算资产名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="参考型号" prop="model">
                <el-input v-model="item.model" placeholder="请输入参考型号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="参考规格" prop="spec">
                <el-input v-model="item.spec" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="计量单位" prop="units">
                <el-input v-model="item.units" placeholder="请输入计量单位" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="预算数量" prop="anCount">
<!--                @change="calcBudgetAmount(item.anCount, item.priceCny, item.budgetAmount)"-->
                <el-input v-model="item.anCount" placeholder="请输入预算数量" @change="calcBudgetAmount(item.anCount, item.priceCny, index)"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="预算单价" prop="priceCny">
                <el-input v-model="item.priceCny" placeholder="请输入预算单价" @change="calcBudgetAmount(item.anCount, item.priceCny, index)">
                  <template slot="append">元</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="预算总价" prop="budgetAmount">
                <el-input v-model="item.budgetAmount" placeholder="请输入预算总价" >
                  <template slot="append">元</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="item.remark" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
        <el-row>
          <el-col :span="24">
            <el-button :loading="buttonLoading" icon="el-icon-circle-plus-outline" type="text" @click="addItem" circle>添加明细</el-button>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm(1)">提 交</el-button>
        <el-button :loading="buttonLoading" @click="submitForm(0)">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情经费申报对话框 -->
    <el-dialog :title="showTitle" :visible.sync="showOpen" append-to-body>
      <el-descriptions :title="showTitle" label-style="width: 110px" :column="2" border>
        <el-descriptions-item label="经费编号" span="2">
          {{ showData.fsubNo }}
        </el-descriptions-item>
        <el-descriptions-item label="经费名称" span="2">
          {{ showData.fsubName }}
        </el-descriptions-item>
        <el-descriptions-item label="经费类别">
          <!-- 经费类别：01.教学 02.纵向调研 03.横向调研 04.专项 -->
          <dict-tag :options="dict.type.sys_jfsb_fsub_type" :value="showData.fsubType"/>
        </el-descriptions-item>
        <el-descriptions-item label="申请日期">
          {{ parseTime(showData.appDate, '{y}-{m}-{d}') }}
        </el-descriptions-item>
        <el-descriptions-item label="审核状态">
          <!-- 审核状态：0.未审 1.审核中 2.审核完结 3.驳回 4.采购执行 5.合同签订 6.到货验收 -->
          <dict-tag :options="dict.type.sys_jfsb_state" :value="showData.state"/>
        </el-descriptions-item>
        <el-descriptions-item label="部门名称">
          {{ showData.deptName }}
        </el-descriptions-item>
        <el-descriptions-item label="部门负责人">
          {{ showData.fzrName }}
        </el-descriptions-item>
        <el-descriptions-item label="项目负责人">
          {{ showData.fsubFzrName }}
        </el-descriptions-item>
        <el-descriptions-item label="申请人">
          {{ showData.appPer }}
        </el-descriptions-item>
        <el-descriptions-item label="联系电话">
          {{ showData.tel }}
        </el-descriptions-item>
        <el-descriptions-item label="申请类别">
          <!-- 申请类别编号：01.常规类申报 02.大型仪器申报 03.科研零散类申报 -->
          <dict-tag :options="dict.type.sys_jfsb_rt_type" :value="showData.rtNo"/>
        </el-descriptions-item>
        <el-descriptions-item label="是否进口">
          <!-- 0.国产 1.进口 -->
          <dict-tag :options="dict.type.sys_jfsb_is_import" :value="showData.isImport"/>
        </el-descriptions-item>
        <el-descriptions-item label="申报理由" span="2">
          {{ showData.applyReason }}
        </el-descriptions-item>
        <el-descriptions-item label="预算年度">
          {{ showData.yusnd }}
        </el-descriptions-item>
        <el-descriptions-item label="预算金额">
          {{ showData.budget }}
        </el-descriptions-item>
        <el-descriptions-item label="备注" span="2">
          {{ showData.remark }}
        </el-descriptions-item>
        <el-descriptions-item label="经费申报明细" span="2" v-for="item in showData.items">
          <el-row>
            <el-col :span="12">
              预算资产编号：
              <span>{{ item.asNo }}</span>
            </el-col>
            <el-col :span="12">
              预算资产名称：
              <span>{{ item.asName }}</span>
            </el-col>
            <el-col :span="12">
              单位名称：
              <span>{{ item.deptName }}</span>
            </el-col>
            <el-col :span="12">
              参考型号：
              <span>{{ item.model }}</span>
            </el-col>
            <el-col :span="12">
              参考规格：
              <span>{{ item.spec }}</span>
            </el-col>
            <el-col :span="12">
              计量单位：
              <span>{{ item.units }}</span>
            </el-col>
            <el-col :span="12">
              预算数量：
              <span>{{ item.anCount }}</span>
            </el-col>
            <el-col :span="12">
              预算单价：
              <span>{{ item.priceCny }}</span>
            </el-col>
            <el-col :span="12">
              预算总价：
              <span>{{ item.budgetAmount }}</span>
            </el-col>
            <el-col :span="24">
              备注：
              <span>{{ item.remark }}</span>
            </el-col>
          </el-row>
        </el-descriptions-item>
        <el-descriptions-item label="审核进度" span="2" v-for="audit in showData.audits">
          <el-row>
            <el-col :span="8">
              <template v-for="(item, index) in dict.type.sys_shb_shtz">
                <template v-if=" audit.shtz == item.raw.dictSort">
                  状态：<span>{{ item.label }}</span>
                </template>
              </template>
            </el-col>
            <el-col :span="8">
              审核人：
              <span>{{ audit.shr }}</span>
            </el-col>
            <el-col :span="8">
              审核日期：
              <span>{{ parseTime(audit.shrq, '{y}-{m}-{d}') }}</span>
            </el-col>
            <el-col :span="24">
              审核意见：
              <span>{{ audit.shyj }}</span>
            </el-col>
          </el-row>
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="showOpen = false">确 定</el-button>
        <el-button @click="showOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, deptTreeSelect } from "@/api/system/user";
import { listJfsb, getJfsb, delJfsb, addJfsb, updateJfsb } from "@/api/yscggl/jfsb";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import "@/assets/styles/vue-treeselect.css";

export default {
  name: "Jfsb",
  dicts: ['sys_jfsb_fsub_type', 'sys_jfsb_state', 'sys_jfsb_rt_type', 'sys_jfsb_is_import', 'sys_shb_shtz'],
  components: { Treeselect },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 经费申报表格数据
      jfsbList: [],
      // 弹出层标题
      title: "",
      // 详细信息标题
      showTitle: "经费申报明细",
      // 是否显示弹出层
      open: false,
      // 是否显详细信息
      showOpen: false,
      // 部门树选项
      deptOptions: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fsubNo: undefined,
        fsubName: undefined,
        fsubType: undefined,
        deptNo: undefined,
        deptName: undefined,
        appPerNo: undefined,
        appPer: undefined,
        tel: undefined,
        appDateArray: undefined,
        state: undefined,
        rtNo: undefined,
        rtName: undefined,
        applyReason: undefined,
        budget: undefined,
        fzrNo: undefined,
        fzrName: undefined,
        isImport: undefined,
        fsubFzrName: undefined,
        fsubFzrNo: undefined,
        fsubTypeName: undefined,
        yusnd: undefined,
      },
      // 表单参数
      form: {
        items: [{
          id: undefined,
          asNo: undefined,
          asName: undefined,
          model: undefined,
          spec: undefined,
          anCount: undefined,
          units: undefined,
          priceCny: undefined,
          budgetAmount: 0.00,
          remark: undefined
        }],
        delItemIds: undefined
      },
      // 详细信息
      showData: {},
      // 表单校验
      rules: {
        // fsubNo: [
        //   { required: true, message: "经费编号不能为空", trigger: "blur" }
        // ]
      },
      columns: [
        { key: 0, label: '主键id', visible: false },
        { key: 1, label: '经费编号', visible: true },
        { key: 2, label: '经费名称', visible: true },
        { key: 3, label: '经费类别', visible: true },
        { key: 4, label: '部门', visible: true },
        { key: 5, label: '申请人', visible: true },
        { key: 6, label: '联系电话', visible: false },
        { key: 7, label: '申请日期', visible: true },
        { key: 8, label: '审核状态', visible: true },
        { key: 9, label: '申请类型', visible: true },
        { key: 10, label: '申报理由', visible: true },
        { key: 11, label: '预算金额', visible: true },
        { key: 12, label: '部门负责人', visible: false },
        { key: 13, label: '是否进口', visible: true },
        { key: 14, label: '项目负责人', visible: false },
        { key: 15, label: '预算年度', visible: true },
        { key: 16, label: '备注', visible: true }
      ],
      // 展开更多检索条件
      queryCollapse: {
        icon: 'el-icon-arrow-down',
        content: '展开',
        open: false
      },
      // 添加还是修改 0.添加 1.修改
      addOrUpdate: 0
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询经费申报列表 */
    getList() {
      this.loading = true;
      listJfsb(this.queryParams).then(response => {
        this.jfsbList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fsubNo: undefined,
        fsubName: undefined,
        fsubType: undefined,
        deptNo: undefined,
        deptName: undefined,
        appPerNo: undefined,
        appPer: undefined,
        tel: undefined,
        appDate: undefined,
        state: undefined,
        rtNo: undefined,
        rtName: undefined,
        applyReason: undefined,
        budget: 0.00,
        fzrNo: undefined,
        fzrName: undefined,
        isImport: undefined,
        fsubFzrName: undefined,
        fsubFzrNo: undefined,
        fsubTypeName: undefined,
        yusnd: undefined,
        remark: undefined,
        items: [],
        delItemIds: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.addOrUpdate = 0;
      this.open = true;
      this.title = "添加经费申报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      this.addOrUpdate = 1;
      const id = row.id || this.ids
      getJfsb(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改经费申报";
      });
    },
    /** 提交按钮 */
    submitForm(state) {
      console.log(this.form);
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.state = state;
          if (state == 1) {
            this.$modal.confirm('经费申报提交后将进行审核并无法修改！确认提交？').then(() => {
              if (this.form.id != null) {
                updateJfsb(this.form).then(response => {
                  this.$modal.msgSuccess("提交成功");
                  this.open = false;
                  this.getList();
                }).finally(() => {
                  this.buttonLoading = false;
                });
              } else {
                addJfsb(this.form).then(response => {
                  this.$modal.msgSuccess("提交成功");
                  this.open = false;
                  this.getList();
                }).finally(() => {
                  this.buttonLoading = false;
                });
              }
            }).catch(() => {
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            if (this.form.id != null) {
              updateJfsb(this.form).then(response => {
                this.$modal.msgSuccess("保存成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addJfsb(this.form).then(response => {
                this.$modal.msgSuccess("保存成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除经费申报编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delJfsb(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yscggl/jfsb/export', {
        ...this.queryParams
      }, `jfsb_${new Date().getTime()}.xlsx`)
    },
    /** 详情按钮操作 */
    handleShow(row) {
      this.loading = true;
      const id = row.id || this.ids
      getJfsb(id).then(response => {
        this.loading = false;
        this.showData = response.data;
        this.showOpen = true;
        this.showTitle = "经费申报详情";
      });
    },
    /** 展开更多查询条件 */
    openMoreParam() {
      if (this.queryCollapse.icon == 'el-icon-arrow-up') {
        this.queryCollapse.open = false;
        this.queryCollapse.icon = 'el-icon-arrow-down';
        this.queryCollapse.content = '展开';
      } else {
        this.queryCollapse.open = true;
        this.queryCollapse.icon = 'el-icon-arrow-up';
        this.queryCollapse.content = '收起';
      }
    },
    /** 添加经费申报明细 */
    addItem() {
      this.form.items.push({
        id: undefined,
        asNo: undefined,
        asName: undefined,
        model: undefined,
        spec: undefined,
        anCount: undefined,
        units: undefined,
        priceCny: undefined,
        budgetAmount: 0.00,
        remark: undefined
      });
    },
    /** 删除经费申报明细 */
    removeItem(item) {
      if (item.id != undefined) {
        this.$modal.confirm('删除后若进行保存或提交操作则经费申报明细不可恢复，是否确认删除经费申报明细？').then(() => {
          if (this.form.delItemIds == undefined) {
            this.form.delItemIds = item.id + ',';
          } else {
            this.form.delItemIds = this.form.delItemIds + item.id + ',';
          }
          const index = this.form.items.indexOf(item)
          if (index !== -1) {
            this.form.items.splice(index, 1)
          }
        });
      } else {
        this.$modal.confirm('是否确认删除经费申报明细？').then(() => {
          const index = this.form.items.indexOf(item)
          if (index !== -1) {
            this.form.items.splice(index, 1)
          }
        });
      }
    },
    /** 计算总价 */
    calcBudget() {
      for (let i = 0; i < this.form.items.length; i++) {
        this.form.budget = this.form.items[i].budgetAmount + this.form.budget;
      }
    },
    /** 计算总价 */
    calcBudgetAmount(anCount, priceCny, index) {
      if (anCount != undefined && priceCny != undefined && anCount != '' && priceCny != '') {
        this.form.items[index].budgetAmount = anCount * priceCny;
        this.calcBudget();
      }
    }
  }
};
</script>
